package com.sy.module_clockin.model

import android.content.Context
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.asLiveData
import com.blankj.utilcode.util.ToastUtils
import com.fenghuajueli.libbasecoreui.utils.MmkvUtils
import com.fenghuajueli.libbasecoreui.viewmodel.BaseViewModel2
import com.haibin.calendarview.Calendar
import com.haibin.calendarview.CalendarView
import com.sy.module_clockin.annotation.ClockInType
import com.sy.module_clockin.db.ClockInDatabase
import com.sy.module_clockin.db.dbsheet.ClockInData
import com.sy.module_clockin.entity.ContinuesDay
import com.sy.module_clockin.utils.TimeUtils
import kotlinx.coroutines.launch
import java.util.HashMap

class ClockInModel : BaseViewModel2() {


    fun getClockInByTimeRange(context: Context, startTime: Long, endTime: Long) = ClockInDatabase.getInstance(context).clockInDataDao().getClockInByTimeRange(startTime, endTime).asLiveData()

    fun getClockInByTimeRangeAndType(context: Context, @ClockInType clockInType: Int, startTime: Long, endTime: Long) = ClockInDatabase.getInstance(context).clockInDataDao().getClockInByTimeRangeAndType(clockInType, startTime, endTime).asLiveData()
    fun getClockInCount(context: Context) = ClockInDatabase.getInstance(context).clockInDataDao().getClockInCount().asLiveData()

    fun deleteClockInData(context: Context, clockInData: ClockInData) = launch {
        val delete = ClockInDatabase.getInstance(context).clockInDataDao().deleteClockIn(clockInData)
        if (delete == -1) {
            ToastUtils.showShort("删除失败")
        } else {
            ToastUtils.showShort("删除成功")
        }
    }


    fun updateClockInData(context: Context, clockInData: ClockInData) = launch {
        val update = ClockInDatabase.getInstance(context).clockInDataDao().updateClockIn(clockInData)
        if (update == -1) {
            ToastUtils.showShort("更新失败")
        } else {
            ToastUtils.showShort("更新成功")
        }
    }

    fun saveClockInData(context: Context, clockInData: ClockInData) = launch {
        val insert = ClockInDatabase.getInstance(context).clockInDataDao().saveClockIn(clockInData).also {
            clockInData.id = it.toInt()
        }
        if (insert == -1L) {
            ToastUtils.showShort("保存失败")
        } else {
            val continuesDay = MmkvUtils.get("Continuous", ContinuesDay::class.java)

            val currentTime = java.util.Calendar.getInstance().timeInMillis

            if (null == continuesDay) {
                MmkvUtils.save("Continuous", ContinuesDay(currentTime, 1, 1))
            } else {

                val c = java.util.Calendar.getInstance()
                c.timeInMillis = continuesDay.preRecordDate
                TimeUtils.setDayTimeInEnd(c)

                when {
                    currentTime > (c.timeInMillis + 24 * 3600 * 1000) -> {
                        continuesDay.continuesNum = 0
                        continuesDay.accumulateNum += 1
                    }
                    TimeUtils.isInToday(continuesDay.preRecordDate) -> {
                        continuesDay.continuesNum += 0
                    }
                    else -> {
                        continuesDay.continuesNum += 1
                        continuesDay.accumulateNum += 1
                    }
                }

                continuesDay.preRecordDate = currentTime

                MmkvUtils.save("Continuous", continuesDay)
            }

            ToastUtils.showShort("保存成功")
        }
    }

    val schemeDateLiveData = MutableLiveData<MutableMap<String, Calendar>>()

    fun initSchemeDate(list: List<ClockInData>, calendarView: CalendarView) = launch {
        val map: MutableMap<String, Calendar> = HashMap()

        list.forEach {
            val calendar = java.util.Calendar.getInstance()
            calendar.timeInMillis = it.recordTime

            val year: Int = calendar[java.util.Calendar.YEAR]
            val month: Int = calendar[java.util.Calendar.MONTH] + 1
            val day: Int = calendar[java.util.Calendar.DAY_OF_MONTH]

            val schemeCalendar = getSchemeCalendar(year, month, day, getSchemeColor(it.clockInType), "")
            map[schemeCalendar.toString()] = schemeCalendar
        }

        calendarView.setSchemeDate(map)

    }

    private fun getSchemeColor(clockInType: Int): Int {
        return when (clockInType) {
            ClockInType.CLOCKIN_TYPE_GET_UP_EARLY -> {
                0XFF519CEA.toInt()
            }

            ClockInType.CLOCKIN_TYPE_MOVEMENT -> {
                0XFFFBBD78.toInt()
            }

            ClockInType.CLOCKIN_TYPE_DRINK_WATER -> {
                0XFF7AFCFA.toInt()
            }

            ClockInType.CLOCKIN_TYPE_READING -> {
                0XFFFEACAD.toInt()
            }
            else -> 0XFF519CEA.toInt()
        }
    }

    private fun getSchemeCalendar(year: Int, month: Int, day: Int, color: Int, text: String): Calendar {
        val calendar = Calendar()
        calendar.year = year
        calendar.month = month
        calendar.day = day
        calendar.schemeColor = color //如果单独标记颜色、则会使用这个颜色
        calendar.scheme = text
        return calendar
    }
}